package com.mes.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName:SendPageUtil
 * @Description:文件上传工具类
 * @Author cb
 * @Date 2018年2月8日下午3:28:44
 * @version 1.0
 */
public class SendPageUtil {

	/**
	 * 后台向前天操作成功发送状态
	 * @param response
	 * @param effect
	 * @throws Exception
	 */
	public static void sendPage(HttpServletResponse response, int effect)
			throws Exception {
		JSONObject object = new JSONObject();
		if (effect > 0) {
			object.put("success", true);
		} else {
			object.put("success", false);
		}

		ResponseUtil.write(response, object);
	}

	/**
	 * 文件上传
	 * @param files
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static StringBuffer uploadHandle(MultipartFile[] files)
			throws IOException, FileNotFoundException {

		
		StringBuffer buffer = new StringBuffer();
		
		for (MultipartFile mFile : files) {
			//如果没有上传文件,就退出本次上传
			String originalFilename = mFile.getOriginalFilename();
			if (StringUtil.isEmpty(originalFilename)) {
				continue;
			}
			
			//上传文件的类型
			String contentType = mFile.getContentType();
			StringBuffer sb=new StringBuffer();
			sb.append(UUID.randomUUID().toString());
			sb.append(contentType);
			
			String new_path=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
			String pic_path="E:\\image\\";
			
			//创建问价夹Ƿ����
			File file = new File(pic_path+new_path);
			if (!file.exists()) {
				file.mkdirs();
			}
			
			mFile.transferTo(file);
			buffer.append(pic_path+new_path);

		}
		return buffer;
	}
}
